package com.hutao.manage.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.datasource.TargetDataSource;
import com.hutao.manage.mapper.MrPageTypeMapper;
import com.hutao.manage.mapper.ScanFileMapper;
import com.hutao.manage.pojo.MrPageType;
import com.hutao.manage.service.ScanFileService;
@Service
public class ScanFileServiceImpl implements ScanFileService {

	@Autowired
	private ScanFileMapper scanFileMapper;
	
	@Autowired
	private MrPageTypeMapper mrPageTypeMapper;
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public boolean isFileExist(String fileHash) {
		// TODO Auto-generated method stub
		int count = scanFileMapper.getCountByFileHash(fileHash);
		if(count > 0){
			return true;
		}
		return false;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getPaginationScanFilesByFirstGuid(
			String firstGuid) {
		
		
		
		return scanFileMapper.getPaginationScanFilesByFirstGuid(firstGuid);
	}

	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getPaginationScanFilesByFirstGuidAndPagination(String firstGuid,
			String pagination) {
		String unpagination=null;
		if("1".equals(pagination)){
			unpagination=pagination;
			pagination=null;
		}else{
			unpagination=null;
		}
		
		List<Map<String,Object>> mapList = scanFileMapper.getPaginationScanFilesByFirstGuidAndPagination(firstGuid,pagination,unpagination);
		
		for (Map<String, Object> map : mapList) {
			//没有编页过的 按照原图片名称排序 
			if(map.get("originalFileName")!=null){
				String originalFileName = (String)map.get("originalFileName");
				int length = originalFileName.indexOf(".");
				String indexSort = originalFileName.substring(0, length);
				map.put("sortIndex", indexSort);
			}
			
			//已经编页过的 按照主键排序
			if(map.get("mr_page_type_id")!=null){
				int pageTypeId = (int)map.get("mr_page_type_id");
				MrPageType mrPageType = mrPageTypeMapper.selectByPrimaryKey(pageTypeId);
				map.put("dataIndex", mrPageType.getSortCode());
			}
		}
		return mapList;
	}

	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getScanFilePaginationCount(String firstGuid, String unPagination) {
		
		return scanFileMapper.getPaginationFileCount(firstGuid, unPagination);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getPaginationScanWorkStaticCountByNameAndScanDate(String name, String scanStartDate,
			String scanEndDate) {
		int count = scanFileMapper.getPaginationScanWorkStaticCountByNameAndScanDate(name, scanStartDate, scanEndDate);
		return count;
	}
	
	
	
	
	
	
	
	
	

}
